package com.example.by.mdb.activity;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.example.by.mdb.R;
import com.example.by.mdb.db.MyDatabaseHelper;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.Calendar;

/**
 * Created by BY on 2016/1/5.
 */
public class Login extends Activity implements View.OnClickListener {
    ImageView search, cancel;
    Button loginButton;
    RelativeLayout loginLayout;
    EditText loginEdit;
    InputMethodManager imm;
    private MyDatabaseHelper myDatabaseHelper;
    private SQLiteDatabase db;
    private App app;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        init();
        initEmplyee();//将员工信息导入数据库
        deleteOldData();//删除一个月前记录
    }

    private void deleteOldData() {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.DAY_OF_MONTH, c.get(Calendar.DAY_OF_MONTH) - 30);
        String s = c.get(Calendar.YEAR) + "-" + c.get(Calendar.MONTH) + "-" + c.get(Calendar.DAY_OF_MONTH);
        String[] tables = {"pda_check", "pda_maintain", "last_edit", "result_maintain"};
        for (int i = 0; i < tables.length; i++) {
            System.out.println(tables[i] + s);
            Cursor cursor = db.query(tables[i], new String[]{"eqpno"}, "file_name=?", new String[]{tables[i] + s}, null, null, null);
            if (cursor.moveToFirst()) {
                db.delete(tables[i], "file_name=?", new String[]{tables[i] + s});
            }
            cursor.close();
        }
    }

    private void initEmplyee() {
        File file = new File(Environment.getExternalStorageDirectory(), "emply.txt");
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader(file));
            String line = "";
            Cursor cursor = null;
            while ((line = br.readLine()) != null) {
                String[] s = line.split(App.DIVIDE);
                cursor = db.query("employee", null, "worker_no=?", new String[]{s[0].trim()}, null, null, null);
                if (!cursor.moveToFirst()) {
                    db.execSQL("insert into employee(worker_no,name,prop,grade) values(?,?,?,?)", s);
                }
            }
            cursor = db.query("employee", null, null, null, null, null, null);
            System.out.println(cursor.getCount());
            cursor.close();
            br.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void init() {
        app = (App) getApplication();
        myDatabaseHelper = new MyDatabaseHelper(getApplicationContext(), "pda.db");
        db = myDatabaseHelper.getReadableDatabase();
        cancel = (ImageView) findViewById(R.id.loginCancel);
        loginButton = (Button) findViewById(R.id.loginButton);
        loginLayout = (RelativeLayout) findViewById(R.id.loginLayout);
        loginEdit = (EditText) findViewById(R.id.loginEdit);
        imm = (InputMethodManager) loginEdit.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        search = (ImageView) findViewById(R.id.loginSearch);
        search.setOnClickListener(this);
        cancel.setOnClickListener(this);
        loginEdit.setOnClickListener(this);
        loginButton.setOnClickListener(this);
        loginLayout.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.loginButton:
                String workerNo = loginEdit.getText().toString();
                if (!workerNo.equals("")) {
                    Cursor cursor = db.query("employee", null, "worker_no=?", new String[]{workerNo}, null, null, null);
                    if (cursor.moveToFirst()) {
                        String name = cursor.getString(cursor.getColumnIndex("name"));
                        String empno = cursor.getString(cursor.getColumnIndex("worker_no"));
                        String groupNo = cursor.getString(cursor.getColumnIndex("grade"));
                        Intent intent = new Intent(this, MainActivity.class);
                        intent.putExtra("name", name);
                        app.setName(name);
                        app.setGroupNo(groupNo);
//                        System.out.println("dsfsdfsdd"+groupNo);
                        app.setEmpno(empno);
                        startActivity(intent);
                        cursor.close();
                        finish();
                    } else {
                        Toast.makeText(getApplicationContext(), "工号错误", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(getApplicationContext(), "请先输入工号", Toast.LENGTH_SHORT).show();
                }

                break;
            case R.id.loginSearch:
//                Toast.makeText(getApplicationContext(),"search",Toast.LENGTH_SHORT).show();
//                Intent intent = new Intent(this, MainActivity.class);
//                intent.putExtra("name", "袁涌");
//                app.setName("袁涌");
//                app.setEmpno("71657");
//                app.setGroupNo("5");
//                startActivity(intent);
//                finish();
//                break;
            case R.id.loginCancel:
                loginEdit.setText("");
                break;
            case R.id.loginLayout:
                if (imm.isActive()) {
                    imm.hideSoftInputFromWindow(loginEdit.getWindowToken(), 0);
                }
                break;
        }
    }
}
